package com.wanglibao.woodtrade.woodtrade.ui.activity.user;

import android.graphics.Rect;
import android.os.Build;
import android.os.Bundle;
import android.os.CountDownTimer;
import android.os.Handler;
import android.os.Message;
import android.text.TextUtils;
import android.view.View;
import android.view.ViewTreeObserver;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.ScrollView;

import com.wanglibao.woodtrade.woodtrade.R;
import com.wanglibao.woodtrade.woodtrade.config.NetConfig;
import com.wanglibao.woodtrade.woodtrade.jsonrpc.JsonRPCAsyncTask;
import com.wanglibao.woodtrade.woodtrade.ui.activity.BaseActivity;
import com.wanglibao.woodtrade.woodtrade.util.CommonUtil;
import com.wanglibao.woodtrade.woodtrade.util.ToastUtil;

import org.json.JSONException;
import org.json.JSONObject;

import butterknife.Bind;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class ForgetPasswordActivity extends BaseActivity {

    @Bind(R.id.rl_common_title)
    RelativeLayout rlCommonTitle;
    @Bind(R.id.edit_activity_forget_password_phone)
    EditText editActivityForgetPasswordPhone;
    @Bind(R.id.edit_activity_forget_password_password)
    EditText editActivityForgetPasswordPassword;
    @Bind(R.id.edit_activity_forget_password_code)
    EditText editActivityForgetPasswordCode;
    @Bind(R.id.btn_activity_forget_password_getcode)
    Button btn_activity_forget_password_getcode;
    @Bind(R.id.sv_activity_forget_password_view)
    ScrollView sv_activity_forget_password_view;

    TimeCount timeCount;
    String phoneCode = "";
    public boolean isGetCode = false;
    Handler handler = new Handler(new Handler.Callback() {
        @Override
        public boolean handleMessage(Message msg) {
            if (msg.arg2 != 2){
                if (msg.arg1 == NetConfig.TaskId.getUserPhoneCode){
                    isGetCode = true;
                    timeCount = new TimeCount(60000,1000);
                    timeCount.start();
                }else if(msg.arg1 == NetConfig.TaskId.modifyPassword){
                    ToastUtil.createLongToast(ForgetPasswordActivity.this,"密码重置成功,请登录");
                    onBackPressed();
                }
            }
            return false;
        }
    });

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_forget_password);
        ButterKnife.bind(this);
        if (Build.VERSION.SDK_INT >= 19) {
            getWindow().addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
        } else {
            rlCommonTitle.setPadding(0, 0, 0, 0);
        }
        tvTitle.setText("找回密码");
        addViewClicklistener(tvBack);

        if (Build.VERSION.SDK_INT >= 19) {
            final View decorView = getWindow().getDecorView();
            decorView.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
                @Override
                public void onGlobalLayout() {
                    Rect rect = new Rect();
                    decorView.getWindowVisibleDisplayFrame(rect);
                    int screenHeight = decorView.getRootView().getHeight();
                    int height = screenHeight - rect.bottom;
                    LinearLayout.LayoutParams layoutParams = (LinearLayout.LayoutParams) sv_activity_forget_password_view.getLayoutParams();
                    layoutParams.setMargins(0, 0, 0, height);
                    sv_activity_forget_password_view.requestLayout();
                }
            });
        }
    }

    @OnClick(R.id.btn_activity_forget_password_getcode) void onBtnActivityForgetPasswordGetcodeClick() {
        String phone = editActivityForgetPasswordPhone.getText().toString();
        if (!TextUtils.isEmpty(phone)){
            if (CommonUtil.isPhone(phone)){
                JsonRPCAsyncTask jsonRPCAsyncTask = new JsonRPCAsyncTask(this, NetConfig.SERVER+NetConfig.TYPE_USER,
                        NetConfig.getUserPhoneCode,handler,"jsonObject",NetConfig.TaskId.getUserPhoneCode,
                        new Object[]{phone,"2"});
                jsonRPCAsyncTask.execute(new Void[]{});
            }else{
                ToastUtil.createLongToast(this,"手机号填写错误");
            }
        }else{
            ToastUtil.createLongToast(this,"手机号不能为空");
        }
    }

    @OnClick(R.id.btn_activity_forget_password_requst) void onBtnActivityForgetPasswordRegisterClick() {
        String phone = editActivityForgetPasswordPhone.getText().toString();
        String password = editActivityForgetPasswordPassword.getText().toString();
        String code = editActivityForgetPasswordCode.getText().toString();

        if (!TextUtils.isEmpty(phone) && !TextUtils.isEmpty(password) && !TextUtils.isEmpty(code) && isGetCode){
            JSONObject jsonObject  = new JSONObject();
            try {
                jsonObject.put("new_password",password);
                jsonObject.put("phone",phone);
                jsonObject.put("code",code);

            } catch (JSONException e) {
                e.printStackTrace();
            }
            JsonRPCAsyncTask jsonRPCAsyncTask = new JsonRPCAsyncTask(this,NetConfig.SERVER+NetConfig.TYPE_USER,
                    NetConfig.modifyPassword,handler,"jsonObject",NetConfig.TaskId.modifyPassword,
                    new Object[]{jsonObject});
            jsonRPCAsyncTask.execute(new Void[]{});
        }else{
            ToastUtil.createLongToast(this,"资料不完整");
        }


    }

    class TimeCount extends CountDownTimer {
        public TimeCount(long millisInFuture, long countDownInterval) {
            super(millisInFuture, countDownInterval);// 参数依次为总时长,和计时的时间间隔
        }

        @Override
        public void onFinish() {// 计时完毕时触发
            btn_activity_forget_password_getcode.setText("重新获取");
            btn_activity_forget_password_getcode.setClickable(true);
            btn_activity_forget_password_getcode.setBackgroundDrawable(getResources().getDrawable(R.drawable.shape_register_getphonecode));
        }

        @Override
        public void onTick(long millisUntilFinished) {// 计时过程显示
            btn_activity_forget_password_getcode.setClickable(false);
            btn_activity_forget_password_getcode.setText(millisUntilFinished / 1000 + "秒");
            btn_activity_forget_password_getcode.setBackgroundDrawable(getResources().getDrawable(R.drawable.shape_register_getphonecode_noclick));
        }
    }
}
